package com.study.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//第一步 创建资源类
class ShareResource {
    //定义标志位
    // 1 AA     2 BB     3 CC
    private int flag = 1;

    //创建Lock锁
    private Lock lock = new ReentrantLock();

    //创建三个condition
    private Condition c1 = lock.newCondition();
    private Condition c2 = lock.newCondition();
    private Condition c3 = lock.newCondition();

    //打印2次，参数第几轮
    public void print2(int loop) throws InterruptedException {
        //上锁
        lock.lock();
        try {
            //判断：flag值是否是1，如果不是1，等待
            while (flag != 1) {
                //等待
                c1.await();
            }
            //干活：如果flag值是1，就输出信息
            for (int i = 1; i <= 2; i++) {
                System.out.println("当前线程名：" + Thread.currentThread().getName() + " :: " + i + " ：轮数：" + loop);
            }
            //通知：通知BB线程
            //修改标志位为 2 -> BB
            flag = 2;
            //通知BB线程
            c2.signal();
        } finally {
            //释放锁
            lock.unlock();
        }
    }

    //打印4次，参数第几轮
    public void print4(int loop) throws InterruptedException {
        lock.lock();
        try {
            //判断：flag值是否是2，如果不是2，等待
            while (flag != 2) {
                c2.await();
            }
            //干活：如果flag值是2，就输出信息
            for (int i = 1; i <= 4; i++) {
                System.out.println("当前线程名：" + Thread.currentThread().getName() + " :: " + i + " ：轮数：" + loop);
            }
            //通知：通知CC线程
            //修改标志位为 3 -> CC
            flag = 3;
            //通知CC线程
            c3.signal();
        } finally {
            lock.unlock();
        }
    }

    //打印6次，参数第几轮
    public void print6(int loop) throws InterruptedException {
        lock.lock();
        try {
            //判断：flag值是否是3，如果不是3，等待
            while (flag != 3) {
                c3.await();
            }
            //干活：如果flag值是3，就输出信息
            for (int i = 1; i <= 6; i++) {
                System.out.println("当前线程名：" + Thread.currentThread().getName() + " :: " + i + " ：轮数：" + loop);
            }
            //通知：通知AA线程
            //修改标志位为 1 -> AA
            flag = 1;
            //通知AA线程
            c1.signal();
        } finally {
            lock.unlock();
        }
    }
}

public class ThreadDemo3 {
    public static void main(String[] args) {
        ShareResource shareResource = new ShareResource();
        // 创建线程 AA
        new Thread(() -> {
            // 循环调用 3 次
            for (int i = 1; i <= 3; i++) {
                try {
                    shareResource.print2(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "AA").start();
        // 创建线程 BB
        new Thread(() -> {
            // 循环调用 3 次
            for (int i = 1; i <= 3; i++) {
                try {
                    shareResource.print4(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "BB").start();
        // 创建线程 CC
        new Thread(() -> {
            // 循环调用 3 次
            for (int i = 1; i <= 3; i++) {
                try {
                    shareResource.print6(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "CC").start();
    }
}
